"""
日历核心计算模块
处理阳历、阴历转换、闰年判断等复杂历法计算
"""
from datetime import datetime
import pytz
from zhdate import ZhDate

class CalendarCore:
    def __init__(self):
        self.timezone = pytz.timezone('Asia/Shanghai')
        self.current_date = datetime.now(self.timezone)

    def get_current_date(self):
        """获取当前日期(自动更新)"""
        self.current_date = datetime.now(self.timezone)
        return self.current_date

    def is_leap_year(self, year):
        """判断是否为闰年"""
        return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

    def get_month_days(self, year, month):
        """获取指定月份的天数"""
        if month in [4, 6, 9, 11]:
            return 30
        elif month == 2:
            return 29 if self.is_leap_year(year) else 28
        else:
            return 31

    def get_lunar_date(self, year, month, day):
        """获取农历日期"""
        solar_date = datetime(year, month, day)
        lunar_date = ZhDate.from_datetime(solar_date)
        return lunar_date

    def get_weekday(self, year, month, day):
        """获取星期几"""
        return datetime(year, month, day).weekday()
